{{- define "kiali_resources" }}
cpu: 50m
memory: 500Mi
{{- end }}

{{- if (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
---
apiVersion: autoscaling.k8s.io/v1
kind: VerticalPodAutoscaler
metadata:
  name: kiali
  namespace: d8-{{ $.Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "kiali")) | nindent 2 }}
spec:
  targetRef:
    apiVersion: "apps/v1"
    kind: Deployment
    name: kiali
  updatePolicy:
    updateMode: "Auto"
  resourcePolicy:
    containerPolicies:
    - containerName: kiali
      minAllowed:
        {{- include "kiali_resources" . | nindent 8 }}
      maxAllowed:
        cpu: 100m
        memory: 1000Mi
{{- end }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: kiali
  namespace: d8-{{ $.Chart.Name }}
  {{- include "helm_lib_module_labels" (list . (dict "app" "kiali")) | nindent 2 }}
spec:
  {{- include "helm_lib_deployment_strategy_and_replicas_for_ha" . | nindent 2 }}
  revisionHistoryLimit: 2
  selector:
    matchLabels:
      app: kiali
  template:
    metadata:
      labels:
        app: kiali
      annotations:
        checksum/config: {{ include (print $.Template.BasePath "/kiali/configmap.yaml") . | sha256sum }}
    spec:
      {{- include "helm_lib_node_selector" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_tolerations" (tuple . "system") | nindent 6 }}
      {{- include "helm_lib_priority_class" (tuple . "cluster-low") | nindent 6 }}
      {{- include "helm_lib_pod_anti_affinity_for_ha" (list . (dict "app" "kiali")) | nindent 6 }}
      {{- include "helm_lib_module_pod_security_context_run_as_user_custom" (list . 1000 1000) | nindent 6 }}
      serviceAccountName: kiali
      imagePullSecrets:
      - name: deckhouse-registry
      containers:
      - name: kiali
        image: {{ include "helm_lib_module_image" (list $ "kiali") }}
        imagePullPolicy: IfNotPresent
        command:
        - "/opt/kiali/kiali"
        - "-config"
        - "/kiali-configuration/config.yaml"
        ports:
        - name: api-port
          containerPort: 20001
        - name: http-metrics
          containerPort: 9090
        readinessProbe:
          httpGet:
            path: /kiali/healthz
            port: api-port
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 30
        livenessProbe:
          httpGet:
            path: /kiali/healthz
            port: api-port
            scheme: HTTP
          initialDelaySeconds: 5
          periodSeconds: 30
        resources:
          requests:
            {{- include "helm_lib_module_ephemeral_storage_logs_with_extra" 10 | nindent 12 }}
  {{- if not (.Values.global.enabledModules | has "vertical-pod-autoscaler-crd") }}
            {{- include "kiali_resources" . | nindent 12 }}
  {{- end }}
        env:
        - name: ACTIVE_NAMESPACE
          valueFrom:
            fieldRef:
              fieldPath: metadata.namespace
        - name: LOG_FORMAT
          value: json
        - name: LOG_LEVEL
          value: info
        volumeMounts:
        - name: kiali-configuration
          mountPath: "/kiali-configuration"
      volumes:
      - name: kiali-configuration
        configMap:
          name: kiali
